<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" class="custom-title-box">
        <div class="custom-info-title">
          {{getTitleByName(nameOfPermission)}}列表查询
          <el-button link @click="delFav()" style="margin-bottom: 5px;" v-if="userFavorites?.length > 0">
            <svg-icon icon-class="stars" color="#E6A23C" />
          </el-button>
          <el-button link @click="addFav()" style="margin-bottom: 5px;" v-else>
            <svg-icon icon-class="star" />
          </el-button>
        </div>
        <div>
          <el-button v-waves type="success" icon="Search" @click="handleFilter">
            查询
          </el-button>
          <el-button icon="Refresh" @click="handleResetQuery">
            重置
          </el-button>
        </div>
      </el-col>
    </el-row>
    <div class="filter-container">
      <el-form :inline="true" ref="listQuery" :model="listQuery" class="demo-form-inline">
        <el-form-item prop="code" label="合同费用类型编码">
          <InputTagDialog v-model="listQuery.code" placeholder="合同费用类型编码" title="填写合同费用类型编码" />
        </el-form-item>
        <el-form-item prop="name" label="合同费用类型名称">
          <el-input v-model="listQuery.name" placeholder="合同费用类型名称" @keyup.enter="handleFilter" />
        </el-form-item>
        <el-form-item prop="state" label="状态">
          <el-radio-group v-model="listQuery.state">
            <el-radio-button label="">全部</el-radio-button>
            <el-radio-button v-for="(item,index) in getEnumList('CommonEnableDisable')" :label="item.key" :key="index">{{item.value}}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="buildingCode" label="在建工程科目编码">
          <el-input v-model="listQuery.buildingCode" placeholder="在建工程科目编码" @keyup.enter="handleFilter" />
        </el-form-item>
        <el-form-item prop="buildingName" label="在建工程科目名称">
          <el-input v-model="listQuery.buildingName" placeholder="在建工程科目名称" @keyup.enter="handleFilter" />
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="20">
      <el-col :span="24" class="custom-title-box">
        <div class="custom-info-title">列表详情</div>
        <div>
          <!--
          <el-button v-show="checkAuthByCode('')" type="primary" icon="Download" @click="handleExport">
            导出
          </el-button>
          -->
          <el-button type="primary" icon="Edit" :disabled="!checkAuthByCode('')" @click="handleAdd">
            新增
          </el-button>
        </div>
      </el-col>
    </el-row>
    <el-divider class="el-divider--horizontal-margin" />
    <el-table v-loading="listLoading"
              :key="tableKey"
              :data="list"
              :row-style="{height: '0'}"
              :cell-style="{padding: '0'}"
              border
              fit
              highlight-current-row
              tooltip-effect="dark"
              @sort-change="handleSortChange"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="39px" />
      <el-table-column align="center" type="index" label="序号" width="60px" />
      <el-table-column v-if="isDebug" align="center" label="ID" prop="id" width="60px" sortable show-overflow-tooltip />
      <el-table-column align="center" label="合同费用类型编码" prop="code" min-width="205px" sortable show-overflow-tooltip>
        <template v-slot="{row}"><CodeTag :code="row.code" /></template>
      </el-table-column>
      <el-table-column align="center" label="合同费用类型名称" prop="name" min-width="205px" sortable show-overflow-tooltip>
        <template v-slot="{row}">{{ row.name }}</template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="state" min-width="160px" sortable show-overflow-tooltip>
        <template v-slot="{row}">
          <el-switch v-model="row.state"
                     active-value="1"
                     active-text="启用"
                     inactive-value="0"
                     inactive-text="禁用"
                     @change="handleModifyStatus(row)"
                     :disabled="!checkAuthByCode('')" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="在建工程科目编码" prop="buildingCode" min-width="205px" sortable show-overflow-tooltip>
        <template v-slot="{row}">{{ row.buildingCode }}</template>
      </el-table-column>
      <el-table-column align="center" label="在建工程科目名称" prop="buildingName" min-width="205px" sortable show-overflow-tooltip>
        <template v-slot="{row}">{{ row.buildingName }}</template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" width="180px" prop="createTime" sortable show-overflow-tooltip>
        <template v-slot="{row}"><DateTag :timestamp="row.createTime" /></template>
      </el-table-column>
      <el-table-column align="center" label="最后修改时间" width="180px" prop="updateTime" sortable show-overflow-tooltip>
        <template v-slot="{row}"><DateTag :timestamp="row.updateTime" /></template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="160px" fixed="right">
        <template v-slot="{row}">
          <el-button type="primary" link @click="handleView(row)">查看</el-button>
          <el-button v-show="checkAuthByCode('')" type="primary" link @click="handleEdit(row)">编辑</el-button>
          <!--
          <el-popconfirm confirm-button-text="确定"
                         cancel-button-text="取消"
                         :icon="WarningFilled"
                         icon-color="#626AEF"
                         title="您确定删除么？"
                         @confirm="handleDelete(row)"
                         v-if="checkAuthByCode('')">
            <template #reference>
              <el-button type="danger" link>删除</el-button>
            </template>
          </el-popconfirm>
          -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-model:total="total" v-model:page="listQuery.pageNum" v-model:limit="listQuery.pageSize" @pagination="getList" />
    <el-dialog v-model="dialogVisible" :title="dialogTypeTitle" width="70%" top="2vh">
      <el-form class="my-form" ref="obj" :model="obj" :rules="rules" label-position="right" :disabled="dialogType === 'view'" label-width="auto" label-suffix="：">
        <el-form-item v-show="isDebug" label="ID">
          <text>{{ obj.id }}</text>
        </el-form-item>
        <el-form-item label="合同费用类型编码" prop="code">
          <el-input v-model="obj.code" placeholder="请输入合同费用类型编码，最多输入20个字符" />
        </el-form-item>
        <el-form-item label="合同费用类型名称" prop="name">
          <el-input v-model="obj.name" placeholder="请输入合同费用类型名称，最多输入200个字符" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-switch v-model="obj.state" active-value="1" active-text="启用" inactive-value="0" inactive-text="禁用" />
        </el-form-item>
        <el-form-item label="在建工程科目编码" prop="buildingCode">
          <el-input v-model="obj.buildingCode" placeholder="请输入在建工程科目编码，最多输入20个字符" />
        </el-form-item>
        <el-form-item label="在建工程科目名称" prop="buildingName">
          <el-input v-model="obj.buildingName" placeholder="请输入在建工程科目名称，最多输入200个字符" />
        </el-form-item>
        <el-form-item label="创建时间">
          <text>{{ obj.createTime || '自动生成' }}</text>
        </el-form-item>
        <el-form-item label="最后修改时间">
          <text>{{ obj.updateTime || '自动生成' }}</text>
        </el-form-item>
        <el-form-item v-show="isDebug" label="数据版本">
          <text>{{ obj.version }}</text>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="info" icon="Close" @click="dialogVisible=false">关闭</el-button>
        <el-button type="primary" icon="Check" v-show="dialogType !== 'view'" @click="doConfirm('obj')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import baseConfig from '@/mixins/baseConfig'
import {doGet, doPost, doPut, doDelete, doGetDownload} from '@/config/http'
import {alertErrorMsg, customValidator, getListPageSize, notifySuccess, svc, initListUpdate} from '@/config/common'
import InputTagDialog from '@/components/InputTag/inputTagDialog.vue';
import SvgIcon from '@/components/SvgIcon/index.vue';

const emptyObj = {
  code: null, // 合同费用类型编码
  name: null, // 合同费用类型名称
  buildingCode: null, // 在建工程科目编码
  buildingName: null, // 在建工程科目名称
  id: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  deleted: null,
  version: null,
}

export default {
  name: 'contractFeetype_nocache',
  components: {SvgIcon, InputTagDialog},
  mixins: [baseConfig],
  data() {
    const svc = 'icims-api'
    return {
      svc,
      obj: Object.assign({}, emptyObj),
      routes: [],
      nameOfPermission: null,
      dialogVisible: false,
      dialogType: 'add',
      dialogTypeTitle: '新增',
      checkStrictly: false,
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        sortField: 'id',
        sortType: 'desc',
        code: null,
        name: null,
        buildingCode: null,
        buildingName: null,
        createTimeStart: null,
        createTimeEnd: null,
        updateTimeStart: null,
        updateTimeEnd: null,
      },
      selectionList: [],
      dialogFormVisible: false,
      dialogStatus: '',
      rules: {
        code: [{ required: false, validator: customValidator, min: 0, max: 20, label: '合同费用类型编码', trigger: ['blur', 'change'] }],
        name: [{ required: true, validator: customValidator, min: 1, max: 200, label: '合同费用类型名称', trigger: ['blur', 'change'] }],
        buildingCode: [{ required: false, validator: customValidator, min: 0, max: 20, label: '在建工程科目编码', trigger: ['blur', 'change'] }],
        buildingName: [{ required: false, validator: customValidator, min: 0, max: 200, label: '在建工程科目名称', trigger: ['blur', 'change'] }],
      },
    }
  },
  computed: {
    routesData() {
      return this.routes
    },
  },
  created() {
    this.nameOfPermission = 'contractFeetype'
    this.getFavorite(this.nameOfPermission)?.then(res => this.userFavorites = res)
    this.initList()
    this.getList()
  },
  methods: {
    getListPageSize,
    async initList() {
    },
    getList() {
      this.listLoading = true
      doGet(svc, '/contract-feetype/page', this.listQuery).then(res => {
        this.listLoading = false
        this.list = res['records']
        this.total = parseInt(res['total'])
      }).catch(e => {
        console.error(e)
        this.listLoading = false
      })
    },
    handleResetQuery() {
      this.resetForm("listQuery")
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleModifyStatus(row) {
      doPut(svc, '/contract-feetype/modify', row, false).then(() => {
        notifySuccess('操作成功')
        this.getList()
      })
    },
    handleSelectionChange(data) {
      this.selectionList = data
    },
    handleSortChange(data) {
      const { prop, order } = data
      this.listQuery.sortField = prop
      this.listQuery.sortType = order === 'ascending' ? 'asc' : 'desc'
      this.handleFilter()
    },
    handleEdit(row) {
      this.dialogType = 'edit'
      this.dialogTypeTitle = '编辑'
      this.dialogVisible = true
      this.checkStrictly = true
      this.obj = Object.assign({}, row)
    },
    handleView(row) {
      this.dialogType = 'view'
      this.dialogTypeTitle = '详情'
      this.dialogVisible = true
      this.checkStrictly = false
      this.obj = Object.assign({}, row)
    },
    handleAdd() {
      this.dialogType = 'add'
      this.dialogTypeTitle = '新增'
      this.dialogVisible = true
      this.checkStrictly = true
      this.obj = Object.assign({}, emptyObj)
    },
    handleExport() {
      doGetDownload(svc, '/contract-feetype/export', this.listQuery)
    },
    handleDelete(row) {
      doDelete(svc, `/contract-feetype/${row.id}`, {}).then(() => {
        notifySuccess('操作成功')
        this.getList()
        this.dialogVisible = false
        initListUpdate('contractFeetypeList', '/contract-feetype/page')
      })
    },
    async doConfirm(formName) {
      let v = await this.$refs[formName].validate().catch((err)=>{return err})
      if (v !== true) {
        alertErrorMsg('表单验证不通过，请检查数据。')
        return
      }
      switch (this.dialogType) {
        case 'add': {
          doPost(svc, '/contract-feetype/add', this.obj).then(() => {
            notifySuccess('新增成功')
            this.getList()
            this.dialogVisible = false
            initListUpdate('contractFeetypeList', '/contract-feetype/page')
          })
          break
        }
        case 'edit': {
          doPut(svc, '/contract-feetype/modify', this.obj).then(() => {
            notifySuccess('修改成功')
            this.getList()
            this.dialogVisible = false
            initListUpdate('contractFeetypeList', '/contract-feetype/page')
          })
          break
        }
      }
    }
  }
}
</script>
